package com.lyw.mall.gateway.filters;

import com.lyw.mall.gateway.service.impl.UserDetailsServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

@Slf4j
public class JwtTokenAuthenticationFilter implements WebFilter {

    private UserDetailsServiceImpl userDetailService;

    public JwtTokenAuthenticationFilter(UserDetailsServiceImpl userDetailService) {
        this.userDetailService = userDetailService;
    }

    @Override
    public Mono<Void> filter(ServerWebExchange serverWebExchange, WebFilterChain webFilterChain) {
//        ServerHttpRequest serverHttpRequest = serverWebExchange.getRequest();
//        String token = resolveToken(serverWebExchange.getRequest());
        /*if (StringUtils.hasLength(token)) {
            Authentication authentication = jwtTokenUtil.par(token);
            if (authentication != null) {
                String org = serverHttpRequest.getHeaders().getFirst(HEADER_ORG);
                String username = authentication.getName();
                boolean success = userDetailService.checkUserRoleResource(serverHttpRequest.getURI().getPath(), serverHttpRequest.getMethodValue(), username, org);
                if (!success) {
                    ServerHttpResponse response = serverWebExchange.getResponse();
                    response.setStatusCode(HttpStatus.FORBIDDEN);
                    return response.setComplete();
                }
                ServerHttpRequest mutableReq = serverHttpRequest.mutate()
                        .header(HEADER_USER, username).build();
                ServerWebExchange mutableExchange = serverWebExchange.mutate().request(mutableReq).build();

                return webFilterChain.filter(mutableExchange).subscribeOn(Schedulers.boundedElastic())
                        .subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication))
                        .doOnCancel(() -> {
                            log.info("Cancelled");
                        })
                        .doOnSuccess(aVoid -> {
                            log.info("Success");
                        })
                        .doOnError((error) -> {
                            log.error("An error occurred", error.getMessage());
                        })
                        .log()
                        .doOnTerminate(() -> log.info("terminal"))
                        .doAfterTerminate(() -> log.info("afterTerminate"));

            }
        }*/
        System.out.println("123123");
        return webFilterChain.filter(serverWebExchange);
    }

    /**
     * 从请求头中获取token
     *
     * @param request
    private String resolveToken(ServerHttpRequest request) {
        String token = request.getHeaders().getFirst(Constant.TOKEN);
        if (StringUtils.hasLength(token))
            return token.replace(TOKEN_PREFIX, "");
        return null;
    }*/
}
